<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyId">
              <SelectCompanyInput v-model="model.companyId" :max-length="36"  placeholder="请选择公司" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="员工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
              <select-user-by-dep
                placeholder="请选择员工"
                :disabled="formDisabled"
                v-model="model.employeeId"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postCode">
              <j-search-select
                        :disabled="formDisabled"
                        v-model="model.postCode"
                        dict="sys_position,name,code,del_flag = 0 and audit_status=2"
                        placeholder="请选择岗位"
                      />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workingTime">
              <j-date :disabled=formDisabled placeholder="请选择任职时间" v-model="model.workingTime" :show-time="true"
                          date-format="YYYY-MM-DD"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea
                    :disabled="formDisabled"
                    v-model="model.remark"
                    rows="4"
                    placeholder="请输入备注"
                    :max-length="200"
                  />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import JSearchSelect from '@/views/scy/components/JSearchSelect.vue'
  import { mapGetters } from 'vuex'
  import moment from 'moment'

  export default {
    name: 'ErpCompanyEmployeePostDetailForm',
    components: {
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput,
      JSearchSelect
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
    },
    data () {
      var validatorPost = async (rule,value,callback)=>{
        if(value){
          const that = this;
          let companyId=that.model.companyId
          let postCode=that.model.postCode;
          let employeeId=that.model.employeeId;
          if(companyId&&postCode&&employeeId){
              let params={};
            params.companyId=companyId;
            params.employeeId=employeeId;
            params.postCode=postCode;
            // params.postDataSource = '33';
            await getAction(that.url.list, params).then(res => {
              if (res.success) {
                let result=res.result.records
                if(result.length>1){  //新增 
                  callback('员工在当前公司下已存在该岗位！')  
                  return;
                }else if(result.length==1 && !this.model.id){ //新增
                  callback('员工在当前公司下已存在该岗位！')
                  return;
                }else if(result.length==1  && this.model.id!=result[0].id){  //编辑
                  callback('员工在当前公司下已存在该岗位！')
                  return;
                }else{
                  this.$refs['form'].clearValidate('employeeId');
                  this.$refs['form'].clearValidate('postCode');
                  callback()
                  return ;
                }
              }
            }).catch(err => {
              // callback('查询失败')
              return;
            })
          } else {
            callback()
            return;
          }
        }
        callback()
      };
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          employeeId: [
            { required: true, message: '请选择员工!'},
            // { validator: validatorPost, trigger: "change" },
          ],
          workingTime: [
            { required: true, message: '请选择任职时间!'},
            // { validator: validatorPost, trigger: "change" },
          ],
        postCode: [
            { required: true, message: '请选择岗位!'},
            { validator: validatorPost, trigger: "change" },
          ],
        },
        url: {
          // add: "/api/erp/files/company/erpCompanyEmployeePostDetail/add",
          add: "/api/erp/files/employee/erpEmployeePostChangeRecord/add",
          // edit: "/api/erp/files/company/erpCompanyEmployeePostDetail/edit",
          edit: "/api/erp/files/employee/erpEmployeePostChangeRecord/edit",
          queryById: "/api/erp/files/company/erpCompanyEmployeePostDetail/queryById",
          // list: "/api/erp/files/company/erpCompanyEmployeePostDetail/list
          list: "/api/erp/files/employee/erpEmployeePostChangeRecord/queryPostRecordByVo"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.postDataSource = 'company'
            this.model.isVirtual = 1
            this.model.workingTime = moment(this.model.workingTime).format('YYYY-MM-DD HH:mm')
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
              that.confirmLoading = false
               that.$emit('unButtonDisable');
             }).finally(() => {
               that.confirmLoading = false;
             })
          }else {
           that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
